<?php
namespace app\agent\controller;

use app\common\service\AddrService;
use app\agent\service\WkSellerexService;
use app\agent\service\WkSellerService;

/**
 * 商家列表管理
 * @module 商家列表管理
 * @controller 商家列表
 */
class WkSellerController extends BaseController
{
	private $wkSeller = null;

	private $addr = null;

	private $wkSellerex = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->wkSeller = new WkSellerService();

		$this->addr = new AddrService();

		$this->wkSellerex = new WkSellerexService();
	}
	
	/**
	 * @action 商家列表
	 */
	public function page()
	{
        $request              = input('param.');
        $request['type']      = session('type');
        $request['addr_code'] = session('addr_code');

		$data = $this->wkSeller->getViewPage($request);

		//获取下级下拉列表
		$addr[$this->getNowType($request['type'])] = $this->addr->getAddrByPcode($request['addr_code']);

		//下级下拉列表选择了则返回下下级下拉列表
		if( empty($request['city']) === false )
        {
            $addr['county'] = $this->addr->getAddrByPcode($request['city']);
        }

		$this->assign('request', $request);
		$this->assign('data', $data);
		$this->assign('addr', $addr);

		return $this->fetch('page');
	}
	
	
	/**
	 * @action 商家详情
	 */
	public function detail()
	{
		$request = input('param.');
		
		$data = $this->wkSeller->getViewDetail($request);

		$this->assign('data',$data);
		
		return $this->fetch();
	}

    /**
     * @action 商家推广展示详情
     */
    public function wkSelerexDetail()
    {
        $request = input('param.');

        $data = $this->wkSellerex->getViewDetail($request);

        $this->assign('data',$data);

        return $this->fetch('wk_sellerex/detail');
	}

	/*
	 * @action 导出excel
	 */
    public function excel()
    {
        $request = input('param.');

        $data = $this->wkSeller->getExcel(json_decode($request['request'], true));

        $filename = date('Y-m-d H:i:s', time());

        //标题
        $titles = ['店铺名', '商铺号', '手机号', '店铺地址', '进件时间', '经纪人姓名', '经纪人编号', '经纪人手机'];

        $content = [];
        foreach ($data['data'] as $key => $val)
        {
            $content[$key][] = $val['merchantShortName'];
            $content[$key][] = $val['user_id'];
            $content[$key][] = $val['mch_phone'];
            $content[$key][] = $val['address'];
            $content[$key][] = $val['checkTime'] ?? '暂无';
            $content[$key][] = $val['truename'] ?? '暂无';
            $content[$key][] = $val['tgnumber'] ?? '暂无';
            $content[$key][] = $val['telephone'] ?? '暂无';
        }

        $content = array_values($content);

        exportExcelXLSW($filename,$titles, $content, 20);
    }
	
}